package project.server.handlers;

import java.util.LinkedList;

import project.server.util.Logger;
import project.shared.messages.Message;

public class OutboundMessages {
	/** The message list for outbound messages */
	private LinkedList<Message> messages = new LinkedList<Message>();
	
	/** The logger for logging errors. */
	private Logger logger;
	
	/**
	 * Instantiates a new OutboundMessages.
	 *
	 * @param logger the logger
	 */
	public OutboundMessages(Logger logger){
		this.logger = logger;
	}
	
	/**
	 * Adds the message to the messages List.
	 *
	 * @param msg the message to be added
	 */
	public synchronized void addMessage(Message msg){
		messages.add(msg);
		this.notifyAll();
		
	}
	
	/**
	 * Gets the first message and removes it from the list.
	 *
	 * @return the message
	 */
	public synchronized Message getMessage(){
		try{
			while(messages.isEmpty())
				this.wait();
		}catch(InterruptedException e){
			logger.logError("OutboundMessages - getMessage() - wait interrupted");
		}
		
		Message msg = messages.getFirst();
		messages.removeFirst();
		return msg;
	}
}
